-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
suite: fix SetupSubTest and TearDownSubTest execution order #1471
suite: fix SetupSubTest and TearDownSubTest execution order #1471
Conversation
suite/suite.go
Outdated
if tearDownSubTest, ok := suite.s.(TearDownSubTest); ok { | ||
tearDownSubTest.TearDownSubTest() | ||
} | ||
}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the if
outside of the defer
:
if tearDownSubTest, ok := suite.s.(TearDownSubTest); ok {
defer tearDownSubTest.TearDownSubTest()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Also I took the liberty to extract the defer-function in line 102. Please note if I should revert that. I think it should not produce noticeable change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need test that enforces the order. That test should fail before your fix and succeed after.
done |
@linusbarth Please drop the last commit (merge commit). Use |
4f8dd94
to
7d0eaf9
Compare
suite/suite_test.go
Outdated
@@ -259,10 +259,14 @@ func (suite *SuiteTester) TestSubtest() { | |||
|
|||
func (suite *SuiteTester) TearDownSubTest() { | |||
suite.TearDownSubTestRunCount++ | |||
// We should get the *testing.T for the test that is to be torn down |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this comment as 3rd argument of Contains below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
suite/suite_test.go
Outdated
} | ||
|
||
func (suite *SuiteTester) SetupSubTest() { | ||
suite.SetupSubTestRunCount++ | ||
// We should get the *testing.T for the test that is to be set up |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this comment as 3rd argument of Contains below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
33e63d9
to
c46c72b
Compare
There were two problems with the order of execution in the Suite.Run() method: - One could not access the correct testing context ("s.T()") inside the SetupSubTest and TearDownSubTest methods. If the testing context was used for e.g. assertions of mocks in the TearDownSubTest, the results would not be "attached" to the correct test in the test output. - The behavior was different to the order of execution for "root" tests (see lines 167-201) regarding the SetupTest and TearDownTest methods. This could confuse users of the library. Also the logic to be deferred was joined together. This was fine beforehand because a panic in the TearDownSubTest would have had no influence on the "suite.SetT(oldT)". Now since a panic in the TearDownSubTest would lead to omitting the "suite.SetT(oldT)" this defer was split into two separate defers.
c46c72b
to
ac5cd69
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I'd be really happy to see this fix get in.
suite/suite_test.go
Outdated
@@ -259,10 +259,12 @@ func (suite *SuiteTester) TestSubtest() { | |||
|
|||
func (suite *SuiteTester) TearDownSubTest() { | |||
suite.TearDownSubTestRunCount++ | |||
suite.Contains(suite.T().Name(), "subtest", "We should get the *testing.T for the test that is to be torn down") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updating the test suite attributes here and performing assertions alongside the others in the main test body will be more consistent with the current testing setup:
Lines 154 to 172 in ac5cd69
SetupSuiteRunCount int | |
TearDownSuiteRunCount int | |
SetupTestRunCount int | |
TearDownTestRunCount int | |
TestOneRunCount int | |
TestTwoRunCount int | |
TestSubtestRunCount int | |
NonTestMethodRunCount int | |
SetupSubTestRunCount int | |
TearDownSubTestRunCount int | |
SuiteNameBefore []string | |
TestNameBefore []string | |
SuiteNameAfter []string | |
TestNameAfter []string | |
TimeBefore []time.Time | |
TimeAfter []time.Time |
See this for reference:
https://github.com/stretchr/testify/pull/1393/files#diff-ca4ffdb7050ad570420ac5682fefad8481384709c2884051cd57071283ce9ca4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review! You're right, that is more consistent. I mostly just copied your referenced testing code now into my branch.
This fix adds panic handling for subtests which will achieve: - subtests will fail for the correct test context when panicking - the test execution is not stopped; the next subtest will be executed
Summary
The order of execution of SetupSubTest and TearDownSubTest methods was changed so that the testing context inside these methods is correct.
Changes
Motivation
There were two problems with the order of execution in the Suite.Run() method:
Related issues
Closes #1465